
#include "communityinterface.h"
#include <QSettings>

#include "connection.h"


CommunityInterface::CommunityInterface(void):
        Optra::ZoneInterface(NULL), connected_(false), conn_(NULL)
{
    ui_ = new CommunityUI;

    info_ = new Optra::ZoneInfo("communityZone", "community support");
    info_->setMenu(new QMenu("Community Test menu"));
    info_->addView(new Optra::ZoneWindow("communityZone#Setup", "Community Setup", ui_));
}

CommunityInterface::~CommunityInterface(void)
{    
    delete info_;
}

Optra::ZoneInfo* CommunityInterface::zoneInfo()
{
    return info_;
}

bool CommunityInterface::isConnected()const
{
    return conn_->isConnected();
}

bool CommunityInterface::initialize()
{  
    conn_ = new Connection(0, proxy());
    connect(conn_, SIGNAL(ready()), SLOT(connectionReady()));


    connect(ui_, SIGNAL(connectManual()), SLOT(manualConnection()));
   // p_->registerView(info(), ui_);
    return true;
}

bool CommunityInterface::configure(bool read)
{

    return true;
}

void CommunityInterface::unload()
{
    if (conn_->isActive()){
        conn_->stop();
    }

    delete conn_;    
}

void CommunityInterface::launch()
{
    //  QStringList str;
    //str << "community" << "user" << "password";
    // QStringList v = zoneProperty("database", "values", str).toStringList();

    QString user = settings().value("username").toString();
    QString pass = settings().value("password").toString();

    ui_->setUser(user);
    ui_->setPassword(pass);

    if (!user.isEmpty() && !pass.isEmpty()){
        conn_->setUser(user);
        conn_->setPassword(pass);
        conn_->start();
    }
}

void CommunityInterface::manualConnection()
{
    settings().setValue("username", ui_->user());
    settings().setValue("password", ui_->password());


    conn_->setUser(ui_->user());
    conn_->setPassword(ui_->password());
    conn_->start();
}

void CommunityInterface::connectionReady()
{

    info("Communication Zone ready to work :)");
    connected_ = true;   
    emit communityReady();
}
